package com.healthshare.parse.news;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.healthshare.common.modal.CChannel;
import com.healthshare.common.modal.PChannel;
import com.palmedia.framework.parse.BaseParse;
import com.palmedia.framework.zmfile.ZMFile;
import com.palmedia.framework.zmfile.ZMFilePath;




/**
 * @author xudeliang
 * 资讯解析
 */
public class HealthNewsParse extends BaseParse
{

	public PChannel[] pchanels;
	public HealthNewsParse()
	{
	}
	
	@Override
	public boolean Parse(byte[]data)
	{
		boolean ret = super.Parse(data);
		if(!ret)
		{
			return false;
		}
		if(JSON==parseType)
		{
			ZMFilePath newsPath = new ZMFilePath();
			newsPath.addFileName("healthnews");
			int filesize= ZMFile.getSize(newsPath.toString());
			if(filesize!=data.length)
			{
				ZMFile.delFile(newsPath.toString());
				ZMFile.write(newsPath.toString(), data, 0, data.length, 0);
			}
			try 
			{
				JSONArray ja = jsonObject.getJSONArray("channels");
				int len = ja.length();
				if(len>0)
				{
					pchanels = new PChannel[len];
				}
				for(int i=0;i<len;i++)
				{
					JSONObject iobject =ja.getJSONObject(i);
					//int pid = iobject.getInt("id");
					String pchannelname = iobject.getString("channelname");
					JSONArray childrens =  iobject.getJSONArray("childrens");
					
					int csize = childrens.length();
					CChannel[] childrenarr=null;
					if(csize>0)
					{
						childrenarr = new CChannel[csize];
					}
					for(int j=0;j<csize;j++)
					{
						JSONObject cobject= childrens.getJSONObject(j);
						int cid = cobject.getInt("id");
						String cchannelname = cobject.getString("channelname");
						
						childrenarr[j]=new CChannel();
						childrenarr[j].channelname=cchannelname;
						childrenarr[j].cid=cid;
						
					}
					
					pchanels[i] = new PChannel();
					//pchanels[i].pid=pid;
					pchanels[i].channelname=pchannelname;
					pchanels[i].childrens=childrenarr;
				}
				return true;
			} 
			catch (JSONException e) 
			{
				e.printStackTrace();
			}
		}
		return false;
	}
	
	/*public class PChannel
	{
		public int pid;
		public String channelname;
		public CChannel[] childrens;
		
		//辅助是否是wap
		public boolean isWap=false;
		public String wapUrl;
	}
	
	public class CChannel
	{
		public int cid;
		public String channelname;
	}*/
}
